package com.yunupmall.controller.amdin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunupmall.api.fordVO.BrandVo;
import com.yunupmall.api.fordentity.Brand;
import com.yunupmall.api.po.PagePO;
import com.yunupmall.service.admin.service.BrandService;
import com.yunupmall.uits.Result;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping(value = "/brand")
public class BrandController {
    private final BrandService brandService;
    private final Result result;

    /**
     * 分页
     * @return
     */
    @PostMapping("/page")
    public Result<Page<Brand>> queryDate(@RequestParam(value = "curr") Integer count,@RequestParam(value = "nums") Integer size,@RequestParam(value = "name",required = false) String name) {
       try {
           PagePO pagePO = new PagePO();
           pagePO.setCurrent(count);
           pagePO.setSize(size);
           BrandVo brandVo = new BrandVo();
           if (ObjectUtil.isNotEmpty(name)){
               brandVo.setName(name);
           }
           brandVo.setPagePos(pagePO);
           return result.ok(brandService.pageDate(brandVo));
       }catch (Exception e) {
           return result.error(e.getMessage());
       }
    }

    /**
     * 新增
     * @return
     */
    @PostMapping("/addData")
    public Result<String> addData(@RequestParam(value = "brandName") String brandName) {
        try {
            BrandVo brandVo = new BrandVo();
            brandVo.setName(brandName);
            return result.ok(brandService.addData(brandVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    /**
     * 删除品牌信息
     * @param
     * @return
     */
    @PostMapping("/delete")
    public Result<String> deleteDataId(@RequestParam Integer  id) {
        try {
            BrandVo brandVo = new BrandVo();
            brandVo.setId(id);
            return result.ok(brandService.deleteDate(brandVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    @GetMapping("/getList")
    public Result<List<Brand>> queryBrandData() {
        try {
            return result.ok(brandService.queryBrandData());
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
}
